var Validator = new Class({
    initialize: function(form,options) {
        this.opt(form,options); 
        this.form.addEvent('submit',this.validate.bind(this)); 
    },
    opt: function(form,options) {
        this.form = $(form);
        this.labels = $(form).getElements('label');
        this.params = {
            onStart: null,
            onComplete: null,
            response: '',
            pattern: '',
            patterns: {
                VALID_NOT_ZERO: '/[^0]/',
                VALID_NOT_EMPTY: '/.+/',
                VALID_NUMBER: '/^\d+$/',
                VALID_EMAIL: '/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/'
                }
            };
        this.fields = {}; 
        Object.extend(this.params,options.params || {});
        Object.extend(this.fields,options.fields || {});
    },
    validate: function() {
        for(i in this.fields) { 
            var field = $(i); 
            if(!this.check_field(field, this.fields[i])) {
                this.invalid(field, this.fields[i]);
                return false; 
            } else {
                this.valid(field, this.fields[i]);
            }; 
        }; 
    },
    check_field: function(field, arr) {
        switch(field.nodeName) {
            case 'INPUT':
                switch(field.type) {
                    case 'file':
                    case 'hidden':
                    case 'password':
                    case 'text':
                        return this.type_text(field, arr); 
                        break; 
                    case 'checkbox':
                        return this.type_checkbox(field, arr);
                        break; 
                    case 'submit':
                    case 'button':
                        break;
                };
                break; 
            case 'SELECT':
                return field.multiple ? this.type_multi(field, arr) : this.type_select(field, arr); 
                break;
            case 'TEXTAREA':
                return this.type_text(field, arr); 
                break; 
            case 'DIV':
                return this.type_radio(field, arr); 
                break; 
        };
    },
    type_text: function(field, arr) {
        var value = String($(field).value); 
        var pattern_key = arr.pattern; 
        if(typeof(pattern_key)==='string') {
            var pattern = this.params.patterns[pattern_key]; 
            this.pattern = pattern; 
            this.params.response = arr.response; 
            return value.search(eval(pattern))<0 ? false : true; 
        } else if(typeof(pattern_key)==='object') {
            var ret = false; 
            for(var i=0; i<pattern_key.length; i++) {
            var pattern = this.params.patterns[pattern_key[i]]; 
                ret = value.search(eval(pattern))<0 ? false : true; 
                if(!ret) {
                    this.params.pattern = pattern; 
                    this.params.response = arr.response[i]; 
                    this.pattern = pattern; 
                    return false;           
                }
            }
            return true; 
        }
    },
    type_radio: function(field, arr) {
        var opts = $(field).getElements('input'); 
        var ret = false; 
        for(var i=0; i<opts.length; i++) {
            ret = opts[i].checked===true ? true : ret; 
        }
        this.params.pattern = arr.pattern; 
        this.params.response = arr.response; 
        return ret; 
    },
    type_checkbox: function(field, arr) {
        this.params.pattern = arr.pattern; 
        this.params.response = arr.response; 
        return field.checked; 
    },
    type_multi: function(field, arr) {
        this.params.pattern = arr.pattern; 
        this.params.response = arr.response; 
        return field.selectedIndex<0 ? false : true; 
    },
    type_select: function(field, arr) {
        this.params.pattern = arr.pattern; 
        this.params.response = arr.response; 
        return field.selectedIndex===0 ? false : true; 
    },
    invalid: function(field, arr) {
        alert(this.params.response); 
        field.addClass('error'); 
        this.labels.each(function(item,index){
                if(item.getProperty('for')==field.id) {
                    item.addClass('error'); 
                }
            }); 
        field.ref = this; 
        if(field.nodeName!='SELECT') {
            field.select(); 
        }
        if(this.params.pattern===this.params.patterns.VALID_EMAIL) { 
            field.value = ''; 
        }
        field.focus(); 
        return false; 
    },
    valid: function(field, arr) {
        field.removeClass('error'); 
        this.labels.each(function(item,index){
            item.removeClass('error'); 
            }); 
        return true; 
    }
}); 
